package com.xiaolun;

import com.sun.mail.util.MailSSLSocketFactory;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.security.GeneralSecurityException;
import java.util.Properties;

/**
 * @Author 硕鼠
 * @Date 2020/6/15 17:30
 * @Version 1.0
 * @Description 文件说明
 */
public class SendEmail {


    public static void main(String[] args) throws Exception {
        Properties prop = new Properties();
        prop.setProperty("mail.host", "smtp.qq.com"); //设置qq邮箱服务器
        prop.setProperty("mail.transport.protocol", "smtp"); //邮件发送协议
        prop.setProperty("mail.smtp.auth", "true"); //需要验证用户名和密码

        MailSSLSocketFactory sf = new MailSSLSocketFactory();
        sf.setTrustAllHosts(true);
        prop.put("mail.smtp.ssl.enable", "true");
        prop.put("mail.smtp.ssl.socketFactory", sf);

        Session session = Session.getDefaultInstance(prop,new Authenticator(){
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("3493826518@qq.com", "vghnhyejdnvzcjcd");
            }
        });
        session.setDebug(true);
        Transport ts = session.getTransport();
        ts.connect("smtp.qq.com", "3493826518@qq.com","vghnhyejdnvzcjcd");
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress("3493826518@qq.com"));
        message.setRecipient(Message.RecipientType.TO , new InternetAddress("3493826518@qq.com"));
        message.setSubject("简单的邮件标题");

        //邮件的内容（文本，图片，附件），采用下面的方式。
        //--------------------------------------------------------------------------------------
        //准备文本数据
        MimeBodyPart text= new MimeBodyPart();
        text.setContent("这是一封邮件正文带图片的<img src='cid:bz.jpg'>邮件","text/html;charset=UTF-8");

        //准备图片数据
        MimeBodyPart image = new MimeBodyPart();
        DataHandler dh = new DataHandler(new FileDataSource("D:\\a22IDEA\\javaweb-01-test\\servlet-01\\src\\main\\resources\\1.jpg"));
        image.setDataHandler(dh);
        image.setContentID("bz.jpg");

        //准备附件
        MimeBodyPart attach = new MimeBodyPart();
        attach.setDataHandler(new DataHandler(new FileDataSource("D:\\a22IDEA\\javaweb-01-test\\servlet-01\\src\\main\\resources\\1.txt")));
        //为附件添加名字，不同于图片中的“setContentID”。文件名在邮箱附件上显示。
        attach.setFileName("testTxt.txt");

        //将文本，图片，附件数据进行拼接,描述数据关系
        MimeMultipart allFile = new MimeMultipart();
        allFile.addBodyPart(text);
        allFile.addBodyPart(image);
        allFile.addBodyPart(attach);
        //类型设置为mixed
        allFile.setSubType("mixed");

        //设置到消息中，保存修改
        message.setContent(allFile);
        message.saveChanges();

        //--------------------------------------------------------------------------------------
        ts.sendMessage(message,message.getAllRecipients());
        ts.close();
    }
}
